package com.lingshu.app.ui.report;

import androidx.lifecycle.ViewModelProvider;

import com.lingshu.app.R;
import com.lingshu.app.base.BaseFragment;
import com.lingshu.app.config.Constants;
import com.lingshu.app.databinding.FragmentFormCarotidBinding;
import com.lingshu.app.model.register.RegisterResponse;
import com.lingshu.app.model.register.RegisterScreenshotResponse;
import com.lingshu.core.eventbus.EventBusManager;
import com.lingshu.core.eventbus.EventData;
import com.lingshu.core.eventbus.EventKey;
import com.lingshu.customview.CounterView;

import java.math.BigDecimal;

/**
 * 颈动脉智能报告 Fragment
 */
public class FormCarotidFragment extends BaseFragment<FragmentFormCarotidBinding, FormCarotidFragmentViewModel> {
    /**
     * 获取当前页面的布局资源ID
     *
     * @return 布局资源ID
     */
    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_form_carotid;
    }

    /**
     * 初始化ViewModel
     */
    @Override
    protected void initViewModel() {
        viewModel = new ViewModelProvider(this).get(FormCarotidFragmentViewModel.class);
    }

    /**
     * 绑定ViewModel
     */
    @Override
    protected void bindViewModel() {
        dataBinding.setViewModel(viewModel);
    }

    /**
     * 初始化
     */
    @Override
    protected void init() {
        viewModel.initData();
        if (getArguments() != null) {
            viewModel.registerInfo = (RegisterResponse) getArguments().getSerializable(Constants.REGISTER_INFO);
            viewModel.screenshotResponse1 = (RegisterScreenshotResponse) getArguments().getSerializable(Constants.SCREENSHOT1);
            viewModel.screenshotResponse2 = (RegisterScreenshotResponse) getArguments().getSerializable(Constants.SCREENSHOT2);
            viewModel.loadData();
            viewModel.allSingleReport.postValue(viewModel.registerInfo.getAllowSingleSide());
            viewModel.rightIsConfirm.postValue(viewModel.registerInfo.getRightIsConfirmed());
            viewModel.leftIsConfirm.postValue(viewModel.registerInfo.getLeftIsConfirmed());
        }
        counterListener();
        observeData();
    }

    /**
     * counterView初始化
     *
     * @param counterView
     * @param defaultValue
     * @param minValue
     * @param maxValue
     */
    private void InitCounterView(CounterView counterView, double defaultValue, double minValue, double maxValue) {
        counterView.setMinValue(BigDecimal.valueOf(minValue));
        counterView.setMaxValue(BigDecimal.valueOf(maxValue));
        counterView.setIncrement(BigDecimal.valueOf(0.1D));
        counterView.setDefaultValue(BigDecimal.valueOf(defaultValue));
    }

    /**
     * counter 监听
     */
    private void counterListener() {
        InitCounterView(dataBinding.counterCarotidInnerThicknessRight, 0.8D, 0.0D, 0.9D);
        InitCounterView(dataBinding.counterCarotidThicknessRight, 0.0D, 0.0D, 0.0D);
        InitCounterView(dataBinding.counterCarotidInnerThicknessLeft, 0.8D, 0.0D, 0.9D);
        InitCounterView(dataBinding.counterCarotidThicknessLeft, 0.8D, 0.0D, 0.9D);
        dataBinding.counterCarotidInnerThicknessRight.setOnValueChangedListener(newValue -> viewModel.structureData.getValue().setInnerThicknessCarotidR(newValue.doubleValue()));
        dataBinding.counterCarotidInnerThicknessLeft.setOnValueChangedListener(newValue -> viewModel.structureData.getValue().setInnerThicknessCarotidL(newValue.doubleValue()));
        dataBinding.counterCarotidThicknessRight.setOnValueChangedListener(newValue -> viewModel.structureData.getValue().setThicknessCarotidR(newValue.doubleValue()));
        dataBinding.counterCarotidThicknessLeft.setOnValueChangedListener(newValue -> viewModel.structureData.getValue().setThicknessCarotidL(newValue.doubleValue()));
    }

    /**
     * 数据监控
     */
    private void observeData() {
        //结构化数据
        viewModel.structureData.observe(this, data -> {
            if (data.getExceptionalCarotidR() == -2) {
                InitCounterView(dataBinding.counterCarotidInnerThicknessRight, data.getInnerThicknessCarotidR(), 0.0D, 0.0D);
                InitCounterView(dataBinding.counterCarotidThicknessRight, data.getThicknessCarotidR(), 0.0D, 0.0D);
            } else if (data.getExceptionalCarotidR() == 0) {
                InitCounterView(dataBinding.counterCarotidInnerThicknessRight, data.getInnerThicknessCarotidR(), 0.0D, 0.9D);
                InitCounterView(dataBinding.counterCarotidThicknessRight, data.getThicknessCarotidR(), 0.0D, 0.0D);
            } else if (data.getExceptionalCarotidR() == 1) {
                InitCounterView(dataBinding.counterCarotidInnerThicknessRight, data.getInnerThicknessCarotidR(), 1.0D, 1.4D);
                InitCounterView(dataBinding.counterCarotidThicknessRight, data.getThicknessCarotidR(), 0.0D, 0.0D);
            } else {
                InitCounterView(dataBinding.counterCarotidInnerThicknessRight, data.getInnerThicknessCarotidR(), 0.0D, 1.4D);
                InitCounterView(dataBinding.counterCarotidThicknessRight, data.getThicknessCarotidR(), 1.5D, 20.0D);
            }

            if (data.getExceptionalCarotidL() == -2) {
                InitCounterView(dataBinding.counterCarotidInnerThicknessLeft, data.getInnerThicknessCarotidL(), 0.0D, 0.0D);
                InitCounterView(dataBinding.counterCarotidThicknessLeft, data.getThicknessCarotidL(), 0.0D, 0.0D);
            } else if (data.getExceptionalCarotidL() == 0) {
                InitCounterView(dataBinding.counterCarotidInnerThicknessLeft, data.getInnerThicknessCarotidL(), 0.0D, 0.9D);
                InitCounterView(dataBinding.counterCarotidThicknessLeft, data.getThicknessCarotidL(), 0.0D, 0.0D);
            } else if (data.getExceptionalCarotidL() == 1) {
                InitCounterView(dataBinding.counterCarotidInnerThicknessLeft, data.getInnerThicknessCarotidL(), 1.0D, 1.4D);
                InitCounterView(dataBinding.counterCarotidThicknessLeft, data.getThicknessCarotidL(), 0.0D, 0.0D);
            } else {
                InitCounterView(dataBinding.counterCarotidInnerThicknessLeft, data.getInnerThicknessCarotidL(), 0.0D, 1.4D);
                InitCounterView(dataBinding.counterCarotidThicknessLeft, data.getThicknessCarotidL(), 1.5D, 20.0D);
            }
        });

        //保存报告 触发
        viewModel.reportSaveResponse.observe(this, data -> {
            viewModel.registerInfo = data.getRegisterInfo();
            viewModel.setStructureData(data.getCarotidForm());
            viewModel.structureData.postValue(data.getCarotidForm());
            viewModel.canSave.postValue(true);
            viewModel.canAudit.postValue(true);
        });

        //审核报告 触发
        viewModel.reportAuditResponse.observe(this, data -> {
            viewModel.registerInfo = data;
            viewModel.canSave.postValue(false);
            viewModel.canAudit.postValue(false);

            getActivity().finish();
            EventBusManager.post(new EventData<>(EventKey.Refresh, "refresh"));
        });

        //右侧 变化触发
        viewModel.allowInputR.observe(this, data -> {
            if (!data) {
                if (viewModel.structureData.getValue().getExceptionalCarotidR() == -2) {
                    InitCounterView(dataBinding.counterCarotidInnerThicknessRight, 0.8D, 0.0D, 0.0D);
                    InitCounterView(dataBinding.counterCarotidThicknessRight, 0.0D, 0.0D, 0.0D);
                } else if (viewModel.structureData.getValue().getExceptionalCarotidR() == 0) {
                    InitCounterView(dataBinding.counterCarotidInnerThicknessRight, 0.8D, 0.0D, 0.9D);
                    InitCounterView(dataBinding.counterCarotidThicknessRight, 0.0D, 0.0D, 0.0D);
                } else if (viewModel.structureData.getValue().getExceptionalCarotidR() == 1) {
                    InitCounterView(dataBinding.counterCarotidInnerThicknessRight, 1.2D, 1.0D, 1.4D);
                    InitCounterView(dataBinding.counterCarotidThicknessRight, 0.0D, 0.0D, 0.0D);
                }
            } else {
                InitCounterView(dataBinding.counterCarotidInnerThicknessRight, 1.0D, 0.0D, 1.4D);
                InitCounterView(dataBinding.counterCarotidThicknessRight, 1.5D, 1.5D, 20.0D);
            }
        });

        //左侧 变化触发
        viewModel.allowInputL.observe(this, data -> {
            if (!data) {
                if (viewModel.structureData.getValue().getExceptionalCarotidL() == -2) {
                    InitCounterView(dataBinding.counterCarotidInnerThicknessLeft, 0.8D, 0.0D, 0.0D);
                    InitCounterView(dataBinding.counterCarotidThicknessLeft, 0.0D, 0.0D, 0.0D);
                } else if (viewModel.structureData.getValue().getExceptionalCarotidL() == 0) {
                    InitCounterView(dataBinding.counterCarotidInnerThicknessLeft, 0.8D, 0.0D, 0.9D);
                    InitCounterView(dataBinding.counterCarotidThicknessLeft, 0.0D, 0.0D, 0.0D);
                } else if (viewModel.structureData.getValue().getExceptionalCarotidL() == 1) {
                    InitCounterView(dataBinding.counterCarotidInnerThicknessLeft, 1.2D, 1.0D, 1.4D);
                    InitCounterView(dataBinding.counterCarotidThicknessLeft, 0.0D, 0.0D, 0.0D);
                }
            } else {
                InitCounterView(dataBinding.counterCarotidInnerThicknessLeft, 1.0D, 0.0D, 1.4D);
                InitCounterView(dataBinding.counterCarotidThicknessLeft, 1.5D, 1.5D, 20.0D);
            }
        });
    }
}